/**
 * @Method Description
 * @Author: hao zheng@
 * @Description:In User Settings Edit
 * @LastEditors: Please set LastEditors
 * @param {}
 * @return 
 * @createTime: 2021-7-9 11:15:13
 */
import {
	request
} from '@/common/js/request'
const bassUrl = require('@/common/js/config.js').bass;
import {
	msg
} from "@/common/js/util.js"

const pageData=require('@/common/js/pageData.js');
export default {
	data() {
		return {
			investmentList:pageData.investmentList,
			cartUserTypeList:pageData.cartUserTypeList,
			isLoading: false, //刷新数据
			loaded: false, //加载完毕
			ImageUrl:'http://localhost',
			mapKey:''
		}
	},
	methods: {
		/**
		 * 获取页面栈
		 * @param {Number} preIndex为1时获取上一页
		 * @return {Object} 
		 */
		prePage(preIndex = 1) {
			const pages = getCurrentPages();
			const prePage = pages[pages.length - (preIndex + 1)];
			return prePage.$vm;
		},
		refresh() {
			this.$nextTick(() => {
				this.$refs.pullScroll.refresh();
			});
		},
		/**
		 * @Description point something to window
		 */
		back() {
			setTimeout(() => {
				uni.navigateBack()
			}, 500)
		},
		/**
		 * @Description setNavigationBarTitle
		 */
		setNavTitle(title) {
			if (!title) return;
			uni.setNavigationBarTitle({
				title
			})
		},
		/**
		 * @Description Navigate to  some page of this project and judge user has login
		 * @options {}  judge user has login
		 * @url {}  Navigate to  some page path
		 */
		navTo(url, options = {}) {
			console.log(url)
			this.$util.throttle(() => {
				if (!url) {
					return;
				}
				if (options.login && !uni.getStorageSync('uid')) {
					// #ifdef APP-PLUS
					url = '/pages/login/login';
					// #endif
					// #ifndef APP-PLUS
					url = '/pages/public/login';
					// #endif
				}
				uni.navigateTo({
					url: url
				})
			}, 100)
		},
		redTo(url, options = {}) {
			console.log(url)
			this.$util.throttle(() => {
				if (!url) {
					return;
				}
				if (options.login && !uni.getStorageSync('uid')) {
					// #ifdef APP-PLUS
					url = '/pages/login/login';
					// #endif
					// #ifndef APP-PLUS
					url = '/pages/public/login';
					// #endif
				}
				uni.redirectTo({
					url: url
				})
			}, 100)
		},
		switchTab(url, options = {}) {
			console.log(url)
			this.$util.throttle(() => {
				if (!url) {
					return;
				}
				if (options.login && !uni.getStorageSync('uid')) {
					// #ifdef APP-PLUS
					uni.navigateTo({
						url: '/pages/login/login'
					})
					// #endif
					// #ifndef APP-PLUS
					uni.navigateTo({
						url: '/pages/public/login'
					})
					// #endif
					
				} else {
					uni.switchTab({
						url: url
					})
				}
			}, 100)

		},
		/**
		 * @Description setClipboardData
		 * @content {}  content
		 */
		setClipboardData(content) {
			if (!content) return;
			uni.setClipboardData({
				data: content,
				showToast:false,
				success: function() {
					uni.showToast({
						icon:"none",
						title:"打开淘宝APP进入商品页面下单"
					})
				}
			});
		},
		callphone(phone) {
			console.log(phone)
			this.$util.throttle(() => {
				if (!phone) {
					return;
				}
				uni.makePhoneCall({
					phoneNumber: phone
				})
			}, 100)
		},
		bannerItemTap(item) {
			if (!uni.getStorageSync('uid')) {
				// #ifdef APP-PLUS
				uni.navigateTo({
					url: '/pages/login/login'
				})
				// #endif
				// #ifndef APP-PLUS
				uni.navigateTo({
					url: '/pages/public/login'
				})
				// #endif
				return
			}
			console.log(item)
			//  "type":""//跳转类型0富文本1xx页面2345678910
			getApp().globalData.bannerItem = item;
			if (item.type == 0) {
				this.navTo("/pages/public/picview");
			} else if (item.type == 1) {
				// this.navTo(`/pages/details/goodDetails?id=${item.goods.id}`);
			}
		},
		/**
		 * @Description get user info
		 */
		async getUserInfo() {
			if (!uni.getStorageSync('uid')) return;
			const res = await this.$request('user/info', {
				uid: uni.getStorageSync('uid')
			});
			if (res.result == 0) {
				getApp().globalData.userInfo = res.data;
				if (this.userInfo) this.userInfo = res.data;
			} else {
				this.$util.msg(res.resultNote);
			}
		},
		openMap({longitude,latitude,name,address}){
			uni.openLocation({
				address,
				name,
				longitude:longitude-0,
				latitude:latitude-0,
				success() {
					
				}
			})
		},
		/**
		 * @Description get data from  back-end and judge user has login
		 * @options {}  judge user has login
		 * @url string request back-end interface  need path
		 * @data {}  request back-end interface need parameter
		 */
		$request(url, data = {}, options = {}) {
			if (options.login && !uni.getStorageSync('uid')) {
				// #ifdef APP-PLUS
				this.navTo('/pages/login/login');
				// #endif
				// #ifndef APP-PLUS
				this.navTo('/pages/public/login');
				// #endif
				return
			}
			if (options.showLoading) {
				this.isLoading = true;
			}
			return new Promise((resolve, reject) => {
				request(url, data, options).then(res => {
					if (res.statusCode == 200) {
						resolve(res.data)
					} else {
						this.$util.msg(`错误码:${res.statusCode}`);
						this.$refs.pullScroll && this.$refs.pullScroll.empty();
					}
					setTimeout(() => {
						this.isLoading = false;
						this.loaded = true;
					}, 100)
				}).catch((err) => {
					reject(err);
				})
			})
		},
		/**
		 * @Description upload file to back-end
		 * @url string   request back-end interface  need path
		 * @data {}  request back-end interface need parameter
		 */
		$uploadFile(url, data = {}) {
			console.log(data)
			return new Promise((e, n) => {
				const uploadTask = uni.uploadFile({
					url: encodeURI(bassUrl + '/api/' + url),
					filePath: data.file,
					name: 'file',
					method: "POST",
					header: {
						'Content-Type': 'multipart/form-data'
					},
					success: function(res) {
						200 == res.statusCode ? e(res.data) : msg(`错误码:${res.statusCode}`);
					},
					fail: function(err) {
						"request:fail " === err.errMsg && msg("请求数据失败！"), n(err.data);
					}
				});
				uploadTask.onProgressUpdate((res) => {
					data.progress = res.progress;
				});
			})
		},
		/**
		 * @Description show image is in lazyload but this is only  use  scoll-view
		 * @data  current can show  image
		 */
		imageOnLoad(data, key) {
			setTimeout(() => {
				this.$set(data, 'loaded', true);
			}, 100)
		},
		/**
		 * @Description the templete has  show  do  something
		 * @key string  the templete name  or ref
		 */
		// showPopup(key) {
		// 	this.$util.throttle(() => {
		// 		this.$refs[key].open();
		// 	}, 200)
		// },
		/**
		 * @Description the templete has  hide  do  something
		 * @key string  the templete name  or ref
		 */
		hidePopup(key) {
			this.$refs[key].hidden();
		},
		stopPrevent(event) {
			event.stopPropagation();
		},
		tips(callback,content,title,showCancel=false,confirmText="确定",confirmColor="#0076F6"){
			uni.showModal({
				confirmText,
				content,
				showCancel,
				confirmColor,
				success() {
					callback();
				}
			})
		},
	}
}
